﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement.Security
{
    public partial class ChangePassword : libForm
    {
        private LibManagementEntities context;
        private bool isPwdValid;
        public ChangePassword()
        {
            InitializeComponent();
            context = new LibManagementEntities();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (isPwdValid)
            {
                SecAccountDetail OSecAccountDetail = new SecAccountDetail();
                OSecAccountDetail = context.SecAccountDetails.Single(x => x.AccountId == clsStatics.currentUserId);
                if (txtCurPwd.Text == clsEncyrption.AES.Decrypt(OSecAccountDetail.Password, "tristana#18"))
                {
                    OSecAccountDetail.Password = txtNewPwd.Text;
                    context.SaveChanges();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Current Password is Incorrect.");
                }
            }
        }

        private void IsPasswordValid()
        {
            if (txtNewPwd.Text != txtNewPwd2.Text)
            {
                epPassword.SetError(txtNewPwd2, "Passwords do no match");
                isPwdValid = false;
            }
            else
            {
                epPassword.SetError(txtNewPwd2, "");
                isPwdValid = true;
            }
        }

        private void txtNewPwd2_Validating(object sender, CancelEventArgs e)
        {
            //IsPasswordValid();
        }

        private void txtNewPwd_Validating(object sender, CancelEventArgs e)
        {
            //IsPasswordValid();
        }

        private void txtNewPwd_TextChanged(object sender, EventArgs e)
        {
            IsPasswordValid();
        }

        private void txtNewPwd2_TextChanged(object sender, EventArgs e)
        {
            IsPasswordValid();
        }
    }
}
